package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Set;

/*
* 用户操作实现类
* */
@Service
public class SecurityUserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public User findUserByName(String username) {
        //通过name查询用户
       User user=userDao.findUserByName(username);
       if (user==null){
           return null;
       }
        Integer id = user.getId();
       //通过用户id查询关联的角色
       Set<Role> roles=roleDao.findRoleByUserId(id);

       if (roles!=null && roles.size() > 0){
           for (Role role : roles) {
               Integer id1 = role.getId();
             Set<Permission> permissionSet=permissionDao.findPermissionById(id1);
             if (permissionSet!=null&&permissionSet.size()>0)
             role.setPermissions(permissionSet);
           }
           user.setRoles(roles);
       }
        return user;
    }
}
